package cn.school.fiter;
import cn.school.Entity.Admin;
import cn.school.Service.AdminService;

import java.io.IOException;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebFilter(value = "/*", filterName = "b")
public class Fiter2 implements javax.servlet.Filter {
    public Fiter2() {
    }

    @Override
    public void destroy() {

    }

    @Override
    public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2)
            throws IOException, ServletException {
        AdminService a=new AdminService();
        HttpServletRequest req = (HttpServletRequest) arg0;// 强转
        HttpServletResponse res=(HttpServletResponse)arg1;
        HttpSession h = req.getSession();
        String path = req.getServletPath();// 请求地址
        req.setCharacterEncoding("utf-8");
        if (path.equals("/index.html")||path.equals("/erro.html")||path.substring(path.length()-4).equals(".css")||path.substring(path.length()-3).equals(".js")||path.substring(path.length()-4).equals(".png")){
            arg2.doFilter(arg0, arg1);
            return;
        }
        System.out.println(12);
        if(path.equals("/admin.ao")){
            arg2.doFilter(arg0, arg1);
            return;
        }
        System.out.println(1);
            if(h.getAttribute("admin")==null){
               res.sendRedirect("erro.html");
                return;
            }
        System.out.println(123);
        System.out.println(h.getAttribute("admin"));
        arg2.doFilter(arg0, arg1);// 放行！！！
        return;
    }

    @Override
    public void init(FilterConfig arg0) throws ServletException {
        System.out.println("filter init ");
    }
}

